package cn.hpclub.server.model.product;

import java.io.Serializable;
import java.util.List;

import com.jfinal.ext.plugin.tablebind.TableBind;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Record;

import cn.hpclub.server.constant.Constant;

/**
 * 实体类 - 管理员
 * 
 */
@TableBind(tableName = "sku")
public class Sku extends Model<Sku> implements Serializable{

    private static final long  serialVersionUID = -57555379613217315L;

    public static final String NAME             = "name";

    public static final String OPERATED_BY      = "operated_by";
    public static final String ID               = "id";
    public static final String OPERATED_TIME    = "operated_time";
    public static final String ENABLED          = "enabled";
    public static final Sku    dao              = new Sku();

    /**
     * 
     * @Title: getOpts
     * @Description:获取Sku可选属性值，格式为opt1,opt2,opt3
     * @return String
     */
    public String getOptNames(){
        List<SkuOpt> skuNames = SkuOpt.dao.find("select id, o.name from sku_opt o where o.sku_id=? order by id",
                                                this.getInt("id"));
        StringBuffer sBuffer = new StringBuffer("");
        for(SkuOpt skuopt : skuNames){
            sBuffer.append(skuopt.getStr("name")).append(",");
        }
        if(sBuffer.length() > 0){// 说明至少一个opt值
            String sku = sBuffer.toString();
            return sku.substring(0, sku.lastIndexOf(","));
        }
        return "尚未有属性值，请点击添加...";// 不能返回""，否则添加一个sku后其optNames为空，于是就没有链接，那么他的optNames永远为空
    }

    public boolean checkUnique(int id, String col, String value){
        try{
            List<Record> result = Db.find("select id from " + Constant.Table.SKU + " where " + col + "=?",
                                          new Object[] { value });
            if(result.size() <= 0 || result.get(0).getInt("id") == id){

                return true;
            }

            return false;

        }
        catch(Exception e){
            e.printStackTrace();

            return true;
        }
    }

}
